package com.example.demo.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.Dao.ApplylevDao;
import com.example.demo.Dao.ScoreDao;
import com.example.demo.Model.Course;
import com.example.demo.Model.StuScore;
import com.example.demo.Model.applylev;
import com.example.demo.Model.score;
import com.example.demo.Service.StudentService.ApplylevService;
import com.example.demo.Service.StudentService.ScoreService;
import com.example.demo.Service.TeacherService.CourseService;
import com.example.demo.Service.TeacherService.StuScoreService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class TeacherController {
    @Resource
    private ScoreService scoreservice;
    @Resource
    private CourseService courseservice;
    @Resource
    private StuScoreService stuscoreservice;
    @Resource
    private ApplylevService applylevservice;
    @Resource
    private ApplylevDao applylevdao;

    //显示课程
    @RequestMapping("/show_course")
    public String show_course(HttpSession session){
        JSONObject object=new JSONObject();
        List<Course> course=courseservice.findAll();
        object.put("code",0);
        object.put("msg","");
        object.put("count",course.size());
        object.put("data",course);
        return object.toJSONString();
    }


    //学生成绩展示（展示）（老师）
    @RequestMapping("/show_stuscore")
    public String show_score(HttpSession session){
        String teid=session.getAttribute("teID").toString();
        JSONObject object=new JSONObject();
        List<StuScore> stuscore=stuscoreservice.findByTeID(teid);
        object.put("code",0);
        object.put("msg","");
        object.put("count",stuscore.size());
        object.put("data",stuscore);
        return object.toJSONString();
    }

//    @RequestMapping("/search_stuscore")
//    public String search_stuscore(String coID,HttpSession session){
//        String coid=coID;
//        List<StuScore> stuscore=stuscoreservice.findByCoID(coid);
//        JSONObject object=new JSONObject();
//        object.put("code",0);
//        object.put("msg","");
//        object.put("count",stuscore.size());
//        object.put("data",stuscore);
//        return object.toJSONString();
//    }

    @Resource
    private ScoreDao scoredao;
    //批改分数（编辑）
    @RequestMapping("/addScore1")
    public String addScore1(StuScore stuscore,HttpSession session){
        System.out.println("分数录入");
        StuScore stuscore1=new StuScore();
        stuscore1=stuscore;
        String stuid=new String();
        stuid=stuscore1.getStuID();
        String coid=new String();
        coid=stuscore1.getCoID();
        String score= new String();
        score=stuscore1.getStuscore();
        System.out.println(score);
        System.out.println(coid);
        System.out.println(stuid);
        score sco=new score();
        sco.setStuID(stuid);
        sco.setCoID(coid);
        sco.setStuscore(score);
        scoredao.updateScore(score,stuid,coid);
        return show_score(session);
    }

    //学生请假（展示）（老师）
    @RequestMapping("/show_ApplyLev")
    public String show_ApplyLev(HttpSession session){
        String teid=session.getAttribute("teID").toString();
        System.out.println("请假展示");
        System.out.println(teid);
        //List<applylev> apply=applylevservice.findAll();
        List<applylev> apply=applylevdao.showApply(teid);
        JSONObject object=new JSONObject();
        //
        object.put("code",0);
        object.put("msg","");
        object.put("count",apply.size());
        object.put("data",apply);
        return object.toJSONString();
    }


    //教师审批请假
    //通过
    @RequestMapping("/adopt_Leave/{id}")
    public String adopt_Leave(@PathVariable("id") int id,HttpSession session){
        System.out.println("请假审批");
        System.out.println(id);
        String teID=session.getAttribute("teID").toString();
        System.out.println(teID);
        String situation="通过";

        applylevdao.updateSituation(situation,id);
        JSONObject result=new JSONObject();
        result.put("status","success");
        return result.toJSONString();
    }


    //不通过
    @RequestMapping("/No_adopt_Leave/{id}")
    public String No_adopt_Leave(@PathVariable("id") int id,HttpSession session){
        System.out.println("请假审批");
        System.out.println(id);
        String teID=session.getAttribute("teID").toString();
        System.out.println(teID);
        String situation="不通过";
        applylevdao.updateSituation(situation,id);
        JSONObject result=new JSONObject();
        result.put("sitatus","success");
        return result.toJSONString();
    }
}
